<?php
class links_visit
{
	var $mydirname;
	var $controller_name;
	var $Links;

	/** Constructor
	 *
	**/
	function links_visit($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();
	}

	function index($lid = 0)
	{
		$this->go($lid);
	}

	function go($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {
			if ($this->Links->is_mobile) {
				//PC Attention Page
				$data = array(
					'link'        => $link,
					'mobile_flag' => 'ok',
				);
				$template = $this->Links->getTemplatePrefix() . '_attention.html';
				$this->Links->view($template, $data);
			} else {
				if ($link['adult'] == 1) {
					//Adult Attention Page
					$bread = $this->Links->getBreadcrumbs();
					$bread[] = array('name' => _MD_VISIT_ATTENTION);
					$data = array(
						'link'              => $link,
						'xoops_breadcrumbs' => $bread,
						'mobile_flag'       => 'ok',
					);
					$template = $this->Links->getTemplatePrefix() . '_attention.html';
					$this->Links->view($template, $data);
				} else {
					$this->_jump($link);
				}
			}
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}


	function mobile($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {

			if ($this->Links->is_mobile) {
				if ($link['adult'] == 1) {
					//Adult Attention Page for mobile
					$data = array(
						'link'        => $link,
						'mobile_flag' => 'ok_mobile',
					);
					$template = $this->Links->getTemplatePrefix() . '_attention.html';
					$this->Links->view($template, $data);
				} else {
					$this->_jump($link, TRUE);
				}
			} else {
				//Mobile Attention Page
				$bread = $this->Links->getBreadcrumbs();
				$bread[] = array( 'name' => _MD_VISIT_ATTENTION);
				$data = array(
					'link'              => $link,
					'xoops_breadcrumbs' => $bread,
					'mobile_flag'       => 'ok_mobile',
				);
				$template = $this->Links->getTemplatePrefix() . '_attention.html';
				$this->Links->view($template, $data);
			}

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function ok($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {
			$this->_jump($link);
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function ok_mobile($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {
			$this->_jump($link, TRUE);
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function _jump($link = NULL, $mobile = FALSE)
	{
		global $xoopsConfig;

		if (is_array($link) AND (!empty($link['url']) OR !empty($link['murl']))) {
			//Counter Up
			$this->Links->hitsCounterUp($link['lid']);

			if($mobile == TRUE && !empty($link['murl']) && !headers_sent() && $this->Links->is_mobile) {
				$murl = str_replace('&amp;', '&', $link['murl']);
				header('Location: ' . $murl);
			} else if ($this->Links->module_config['frame'] != "" && ! $mobile && !empty($link['url']) && !$this->Links->is_mobile) {
				$site_name = htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES, _CHARSET);
				$module_name = htmlspecialchars($this->Links->module_name, ENT_QUOTES, _CHARSET);
				$title = sprintf(_MD_VISIT_FRAME_TITLE, $site_name, $module_name, $link['title']);
				header('Content-Type:text/html; charset='._CHARSET);
				header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
				header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
				header('Cache-Control: no-store, no-cache, must-revalidate');
				header("Cache-Control: post-check=0, pre-check=0", false);
				header("Pragma: no-cache");
				echo "<html><head>
					<title>". $title ."</title>
					</head>
					<frameset rows='70px,100%' cols='*' border='0' frameborder='0' framespacing='0' >
					<frame src='". $this->Links->makeUrl('frame_header', 'index', array('lid' => $link['lid'])) ."' frame name='xoopshead' scrolling='no' target='main' Noresize>
					<frame src='". $link['url'] ."' frame name='main' scrolling='auto' target='Main'>
					</frameset></html>";

			} else {
				$url = $mobile ? $link['murl']: $link['url'];
				echo "<html><head><meta http-equiv=\"Refresh\" content=\"0; URL=". $url ."\"></meta></head><body></body></html>";
			}

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

}
